package com.example.app;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.example.netity.order;
import com.example.tool.Config;
import com.example.tool.Utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.TextView;
public class MyApplication extends Application {
	/** test 01 **/
	public static String SERVERPATH = Config.SERVER_PATH;
	public static String qx ;
	public static boolean isdownload = false;
	private static MyApplication mInstance = null;
	public static float density;
	public static int densityDpi;
	public static boolean needJLogin = false;
	public static String device_token;
	private static TypedArray typeArray;
    public  static int index=-2;
    public  static int index2=-2;
	public static Map<String, Thread> mThreads;
	public final static int currentapiVersion = android.os.Build.VERSION.SDK_INT;
	private static List<Activity> mList = new LinkedList<Activity>();
	public static Context applicationContext;
	public static MyApplication instance;
	// login user name
	public final String PREF_USERNAME = "username";
	public static String currentUserNick = "";
	public static String liveName = "";
	public static boolean isLogin = false;

	public static int width, height;
	private static RequestQueue mRequestQueue;
	private String ip = "";
	private String port = "";
	public static String yy;

	public static List<Activity> mylist=new LinkedList<Activity>();
	public static long xtime = 0; // 一体机时间 - 本机时间    秒     差距<60秒 不用设置

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		ip = MyApplication.getServersInfo().getString("serverip", "");
		port = MyApplication.getServersInfo().getString("serverport", "");
		qx = MyApplication.getSharedUserInfo().getString("qx", "");
		MyApplication.SERVERPATH = "http://" + ip + ":" + port;		
		density = getResources().getDisplayMetrics().density;
		densityDpi = getResources().getDisplayMetrics().densityDpi;
		width = getResources().getDisplayMetrics().widthPixels; // 屏幕宽度（像素）
		height = getResources().getDisplayMetrics().heightPixels; // 屏幕高度（像素）
		applicationContext = this;
		instance = this;
		yy= Locale.getDefault().getLanguage();
		yy= MyApplication.getSharedUserInfo().getString("language", yy);
	}
	public static RequestQueue getRequestQueue(){
		if(mRequestQueue==null){
		mRequestQueue=Volley.newRequestQueue(getInstance());
		}
		return mRequestQueue;
		}

	public static boolean isIpv4(String ipAddress) {
		String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
		Pattern pattern = Pattern.compile(ip);
		Matcher matcher = pattern.matcher(ipAddress);
		return matcher.matches();
	}
	public static String registrationId = "";

	private static Typeface typeFaceLight;
	public static String packagename = "esm";

	public static Typeface getLightTypeFace() {
		if (typeFaceLight == null) {
			typeFaceLight = Typeface.createFromAsset(getInstance().getAssets(), "fonts/light.ttf");
		}
		return typeFaceLight;
	}

	public static MyApplication getInstance() {
		return mInstance;
	}


	// add Activity
	public void addActivity(Activity activity) {
		mList.add(activity);
	}

	public static void exit() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}

	public void onLowMemory() {
		super.onLowMemory();
		System.gc();
	}

	static SharedPreferences sharedUserInfo;
	public static String rec = "";

	public static SharedPreferences getSharedUserInfo() {
		if (sharedUserInfo == null) {
			sharedUserInfo = getInstance().getSharedPreferences("test", Context.MODE_PRIVATE);
		}
		return sharedUserInfo;
	}
	static SharedPreferences serversInfo;
	public static SharedPreferences getServersInfo(){
		if (serversInfo == null) {
			serversInfo = getInstance().getSharedPreferences("servers", Context.MODE_PRIVATE);
		}
		return serversInfo;
	}

}
